package org.abratuhi.kvngroupware.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.gui.GUIBrainstorm;
import org.abratuhi.kvngroupware.gui.GUIBrainstormOverview;
import org.abratuhi.kvngroupware.model.Brainstorm;
import org.abratuhi.kvngroupware.network.Message;
import org.jdom.Element;

public class BrainstormController implements ActionListener, WindowListener{
	GUIBrainstorm guibrainstorm;
	Brainstorm kvnbrainstorm;
	KVNGroupwareClient kvnclient;
	
	public BrainstormController(KVNGroupwareClient client, GUIBrainstorm guibrainstorm, Brainstorm kvnbrainstorm){
		this.kvnclient = client;
		this.guibrainstorm = guibrainstorm;
		this.kvnbrainstorm = kvnbrainstorm;
	}

	@Override
	public void windowActivated(WindowEvent e) {}

	@Override
	public void windowClosed(WindowEvent e) {}

	@Override
	public void windowClosing(WindowEvent e) {
		String[] attrNames = new String[]{"type", "action", "brainstormId", "userId", "teamId", "theme"};
		String[] attrValues= new String[]{"brainstorm", "leave", kvnbrainstorm.getId(), kvnclient.getUser().getId(), kvnbrainstorm.getTeamId(), kvnbrainstorm.getTheme()};
		String text = new String();
		Element[] content = new Element[0];
		
		Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
		kvnclient.sendMessage(message);
	}

	@Override
	public void windowDeactivated(WindowEvent e) {}

	@Override
	public void windowDeiconified(WindowEvent e) {}

	@Override
	public void windowIconified(WindowEvent e) {}

	@Override
	public void windowOpened(WindowEvent e) {}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(guibrainstorm.buttonCommit)){
			String[] attrNames = new String[]{"type", "action", "brainstormId", "userId", "teamId", "theme"};
			String[] attrValues= new String[]{"brainstorm", "commit", kvnbrainstorm.getId(), kvnclient.getUser().getId(), kvnbrainstorm.getTeamId(), kvnbrainstorm.getTheme()};
			String text = guibrainstorm.textareaSheet.getText();
			Element[] content = new Element[0];
			
			Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
			kvnclient.sendMessage(message);
		}
		else if(e.getSource().equals(guibrainstorm.menuitemSwitch)){
			String[] attrNames = new String[]{"type", "action", "brainstormId", "userId", "teamId", "theme"};
			String[] attrValues= new String[]{"brainstorm", "switch", kvnbrainstorm.getId(), kvnclient.getUser().getId(), kvnbrainstorm.getTeamId(), kvnbrainstorm.getTheme()};
			String text = new String();
			Element[] content = new Element[0];
			
			Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
			kvnclient.sendMessage(message);
		}
		else if(e.getSource().equals(guibrainstorm.menuitemOverview)){
			new GUIBrainstormOverview(kvnbrainstorm.getOverview()).createJFrame();
		}
	}

}
